package com.cyou.nad.tag.table;


import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;

import com.cyou.nad.datastore.DataList;


/**
 * table 迭代标签
 * @author lzm 
 */
public class TableTag extends TagSupport {

	private static final long serialVersionUID = 2248403295523914310L;
	
	public static final String TABLE_TAG_TRANS="TABLE_TAG_TRANS";
	/**
	 * 数据源
	 */
	private DataList dataList;

	/**
	 * 数据源
	 */
	public void setDataList(DataList dataList) {
		this.dataList = dataList;
	}
	
	/**
	 * 列数据转换器
	 */
	public Collection<TransCol> trans;
	
	
	public Collection<TransCol> getTrans() {
		return trans;
	}

	public void setTrans(Collection<TransCol> trans) {
		this.trans = trans;
	}

	@Override
	public int doStartTag() throws JspException {
		if (dataList == null || dataList.rowCount() <= 0)
			return SKIP_BODY;
		if(trans!=null){
			Map<String, Map<String,String>> tran= new HashMap<String, Map<String,String>>(trans.size());
			for (TransCol t : trans) {
				tran.put(t.colName, t.pairMap);
			}
			pageContext.setAttribute(TABLE_TAG_TRANS, tran);
		}
		return EVAL_BODY_INCLUDE;
	}

	@Override
	public int doEndTag() throws JspException {
		pageContext.removeAttribute(TABLE_TAG_TRANS);
		return EVAL_PAGE;
	}

	/**
	 * 数据源
	 */
	protected DataList getDataList() {
		return dataList;
	}


}